package edu.uwosh.cs342.group2.project4.database;

import java.util.*;

import edu.uwosh.cs342.group2.project4.storage.Category;
import android.content.*;
import android.database.*;
import android.database.sqlite.*;
import android.widget.Toast;

public class CategoryDb extends ObjectDb {
	
	/**
	 * Default constructor for categories
	 * @param context
	 */
	protected CategoryDb(Context context) {
		super(context);
	}
	
	/**
	 * Adds a category to the database
	 * @param category
	 * @return
	 */
	public int AddCategory(String name) {
		int categoryId = 0;
		ContentValues args = new ContentValues();
		args.put("Name", name.trim());
		try {
			categoryId = (int)db.insert("Category", null, args);
		}
		catch (SQLiteException e) {
			Toast.makeText(this.context, "Could not add the category", 25).show();
			categoryId = -1;
		}
		finally {
			this.CloseConnection();
		}
		
		return categoryId;
	}
	
	/**
	 * Gets the category by the unique ID
	 * @param id
	 * @return
	 */
	public Category GetCategory(int id) {
		String[] columns= {
			"Id",
			"Name"
		};
		Category category = null;
		try {
			Cursor set = db.query("Category", columns, "Id = " + String.valueOf(id), null, null, null, null);
			if (set.moveToNext()) {
				category = new Category(
					set.getInt(set.getColumnIndex("Id")),
					set.getString(set.getColumnIndex("Name"))
				);
			}
		}
		catch (SQLiteException ex) {
			Toast.makeText(this.context, "Could not get the category information", 25).show();
			category = null;
		}
		finally {
			this.CloseConnection();
		}
		
		return category;
	}
	
	/**
	 * Gets all of the categories
	 * @return
	 */
	public ArrayList<Category> GetAllCategories() {
		String[] columns= {
			"Id",
			"Name"
		};
		ArrayList<Category> categoryList = new ArrayList<Category>();
		try {
			Cursor set = db.query("Category", columns, null, null, null, null, "Name");
			while (set.moveToNext()) {
				Category category = new Category(
					set.getInt(set.getColumnIndex("Id")),
					set.getString(set.getColumnIndex("Name"))
				);
				categoryList.add(category);
			}
		}
		catch (SQLiteException ex) {
			Toast.makeText(this.context, "Could not get the category information", 25).show();
			categoryList = null;
		}
		finally {
			this.CloseConnection();
		}
		
		return categoryList;
	}
	
	/**
	 * Updates the category's information
	 * @param category
	 * @return
	 */
	public boolean UpdateCategory(Category category) {
		ContentValues args = new ContentValues();
		args.put("Name", category.getName().trim());
		try {
			db.update("Category", args, "Id = " + category.getId(), null);
		}
		catch (SQLiteException e) {
			Toast.makeText(this.context, "Could not update the category information.", 25).show();
			return false;
		}
		finally {
			this.CloseConnection();
		}
		
		return true;
	}
	
	/**
	 * Removes the category from the list
	 * @param id
	 * @return
	 */
	public boolean RemoveCategory(int id) {
		try {
			db.delete("Category", "Id = " + id, null);
		}
		catch (SQLiteException e) {
			Toast.makeText(this.context, "Could not remove the category", 25).show();
			return false;
		}
		finally {
			this.CloseConnection();
		}
		
		return true;
	}
}
